package com.ext.aic.box.logs;

import com.ext.aic.AICManager;
import com.ext.aic.beans.box.ReplyRequestLog;
import com.ext.aic.beans.box.ReplyRequestLog_;
import com.eyinfo.android_pure_utils.snow.Butterfly;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

public class WxReplyRequestLogsManager {

    private static volatile WxReplyRequestLogsManager instance;

    public static WxReplyRequestLogsManager getInstance() {
        if (instance == null) {
            synchronized (WxReplyRequestLogsManager.class) {
                if (instance == null) {
                    instance = new WxReplyRequestLogsManager();
                }
            }
        }
        return instance;
    }

    @Setter
    private boolean enterChatWindow;

    @Getter
    private long logId;

    //是否提前退出
    @Getter
    private boolean earlyExit;

    public void recordLog(ReplyRequestLog log) {
        if (earlyExit) {
            log.setStatus(2);
        }
//        log.setRecordTime(System.currentTimeMillis());
//        AICManager.getInstance().insertOrUpdate(ReplyRequestLog.class, log);
        logId = 0;
        earlyExit = false;
    }

    public List<ReplyRequestLog> findAll() {
        return AICManager.getInstance().findList(ReplyRequestLog.class, queryBuilder -> {
            queryBuilder.orderDesc(ReplyRequestLog_.recordTime);
        });
    }

    public ReplyRequestLog findById(long id) {
        return AICManager.getInstance().findById(ReplyRequestLog.class, id);
    }

    public void clean() {
        //AICManager.getInstance().delete(ReplyRequestLog.class, null);
    }

    public void start() {
        logId = Butterfly.getInstance().nextId();
        earlyExit = false;
    }

    public void exitDetection() {
        if (!enterChatWindow) {
            return;
        }
        enterChatWindow = false;
        if (logId > 0) {
            earlyExit = true;
        }
    }
}
